package m.jclecai.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import m.jclecai.com.R;

import java.util.List;

import m.jclecai.com.adapter.holder.PayRedpacketHolder;
import m.jclecai.com.bean.RedPacketBean;
import m.jclecai.com.listener.NotifyTextViewSetChanged;

/**
 * Created by ZD on 2017/5/11.
 */

public class PayRedpacketAdapter extends RecyclerView.Adapter<PayRedpacketHolder> {
    private Context mContext;
    int redCheckIndex = -1;
    List<RedPacketBean> redPcks;
    NotifyTextViewSetChanged changed;
    public enum FROM{
        BuyRedPkgFragment,LotteryPayActivity
    }
    private FROM from = FROM.LotteryPayActivity;//""默认显示ui是支付界面

    public PayRedpacketAdapter(Context context,List<RedPacketBean> redPcks,NotifyTextViewSetChanged change) {
        this.mContext = context;
        this.redPcks = redPcks;
        this.changed = change;
    }

    @Override
    public PayRedpacketHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PayRedpacketHolder(View.inflate(mContext, R.layout.item_redpacket, null));
    }

    @Override
    public void onBindViewHolder(final PayRedpacketHolder holder, final int position) {
        RedPacketBean redPacketBean = redPcks.get(position);
        redPacketBean.setCheck(position == redCheckIndex);
        holder.init(redPacketBean,from);
        holder.ivPkg.setTag(redPacketBean);
        holder.ivPkg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RedPacketBean redPacketBean = (RedPacketBean)v.getTag();
                if(redPacketBean.isCheck()){
                    holder.ivPkg.setImageResource(R.drawable.redbg1);
                    redCheckIndex = -1;
                }else {
                    holder.ivPkg.setImageResource(R.drawable.redbg2);
                    redCheckIndex = position;
                }
                redPacketBean.setCheck(!redPacketBean.isCheck());
                if(changed != null)
                    changed.onClickSetChangedText();
            }
        });
    }

    @Override
    public int getItemCount() {
        return redPcks.size();
    }


    public int getRedCheckIndex(){
        return redCheckIndex;
    }

    public void setFrom(FROM from) {
        this.from = from;
    }
}
